<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\admin\model\user;

use app\common\model\user\WalletLog as WalletLogModel;

/**
 * 用户个人钱包变动明细模型
 * Class WalletLog
 * @package app\admin\model\user
 */
class WalletLog extends WalletLogModel
{
    /**
     * 获取余额变动明细列表
     * @param array $param
     * @return \think\Paginator
     */
    public function getList(array $param = []): \think\Paginator
    {
        // 设置查询条件
        $filter = $this->getFilter($param);
        // 获取列表数据
        return $this->with(['user.avatar'])
            ->alias('log')
            ->field('log.*')
            ->where($filter)
            ->join('user', 'user.user_id = log.user_id')
            ->order(['log.create_time' => 'desc'])
            ->paginate(15);
    }

    /**
     * 设置查询条件
     * @param array $param
     * @return array
     */
    private function getFilter(array $param): array
    {
        // 设置默认的检索数据
        $params = $this->setQueryDefaultValue($param, [
            'user_id' => 0,         // 用户ID
            'search' => '',         // 用户昵称
            'scene' => 0,           // 余额变动场景
            'betweenTime' => []    // 起止时间
        ]);
        // 检索查询条件
        $filter = [];
        // 用户ID
        $params['user_id'] > 0 && $filter[] = ['log.user_id', '=', $params['user_id']];
        // 用户昵称
        !empty($params['search']) && $filter[] = ['user.name|user.mobile', 'like', "%{$params['search']}%"];
        // 余额变动场景
        $params['scene'] > 0 && $filter[] = ['log.scene', '=', (int)$params['scene']];
        // 起止时间
        if (!empty($params['betweenTime'])) {
            $times = $this->between_time($params['betweenTime']);
            $filter[] = ['log.create_time', '>=', $times['start_time']];
            $filter[] = ['log.create_time', '<', $times['end_time'] + 86400];
        }
        return $filter;
    }

    /**
     * 格式化起止时间(为了兼容前端RangePicker组件)
     * 2020-04-01T08:15:08.891Z => 1585670400
     * @param array $times
     * @param bool $isWithTime 是否包含时间
     * @return array
     */
    public function between_time(array $times, bool $isWithTime = false): array
    {
        foreach ($times as &$time) {
            $time = trim($time, '&quot;');
            $time = $this->str2date($time, $isWithTime);
        }
        return ['start_time' => current($times), 'end_time' => next($times)];
    }

    /**
     * 日期转换时间戳
     * 例如: 2020-04-01 08:15:08 => 1585670400
     * @param string $date
     * @param bool $isWithTime 是否包含时间
     * @return false|int
     */
    public function str2date(string $date, bool $isWithTime = false)
    {
        if (!$isWithTime) {
            $date = date('Y-m-d', strtotime($date));
        }
        return strtotime($date);
    }
}
